﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FytSoa.Application.AutoMapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using FytSoa.Common.Utils;
using FytSoa.CrossCutting;
using FytSoa.CrossCutting.Middleware;
using FytSoa.CrossCutting.Swagger;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Masuit.Tools.Config;
using Microsoft.Extensions.FileProviders;
using System.IO;
using FytSoa.ApiService.Filters;
using FytSoa.Quartz;

namespace FytSoa.ApiService
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            //add Config
            AppConfigUtils.InitConfig (configuration);

            //reg Tools
            configuration.AddToMasuitTools();
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers(options => {
                    options.Filters.Add (typeof (AopActionFilter));
                    //options.Filters.Add (typeof (AopResultFilter));
                })
                .AddNewtonsoftJson(options => {
                options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                //options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            });

            // Register DI
            services.RegisterServices();
            
            // Swagger Config
            services.AddSwaggerConfiguration ();

            // Jwt Config
            services.AddJwtConfiguration ();
            
            // Auto Mapper
            services.AddAutoMapper(typeof(MappingConfigProfile));

            // Quartz
            services.QuartzStartup();

        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwaggerSetup ();
            }

            // open scheduler
            app.ApplicationServices.GetService<IQuartzService>().InitStart();

            //app.UseHttpsRedirection();

            app.UseCors (c => {
                c.AllowAnyHeader ();
                c.AllowAnyMethod ();
                c.AllowAnyOrigin ();
                c.WithExposedHeaders ("RefreshToken");
            });

            app.UseRouting();

            app.UseMiddlewareSetup ();

            app.UseAuthorization();

            app.UseStaticFiles(new StaticFileOptions
            {
                ServeUnknownFileTypes = true,
                FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory())
            });

            app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
        }
    }
}